﻿using System;
// Clases necesarias para la seguridad.
using System.Security.Cryptography;
using System.Text;

namespace CentroVentas.Clases
{
    class Seguridad
    {

        public static string generarClaveSHA1(string cadena)
        {
            UTF8Encoding enc = new UTF8Encoding();
            Byte[] data = enc.GetBytes(cadena);
            
            SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
            Byte[] result = sha.ComputeHash(data);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < result.Length - 1; i++)
            {
                if (result[i] < 16)sb.Append("0");
                sb.Append(result[i].ToString("x"));
            }
            return sb.ToString().ToUpper();
        }

        public static string generarClaveMD5(string cadena)
        {
            UnicodeEncoding ueCodigo = new UnicodeEncoding();
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            Byte[] bHash = md5.ComputeHash(ueCodigo.GetBytes(cadena));
            return Convert.ToBase64String(bHash);
        }

        public static string generarClaveSHA256(string cadena)
        {
            SHA256 sha = new SHA256Managed();
            Byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(cadena));

            StringBuilder strBuilder = new StringBuilder();
            foreach (Byte b in hash)
            {
                strBuilder.AppendFormat("{0:x2}", b);
            }
            return strBuilder.ToString();
        }

        public static string generarClaveSHA384(string cadena)
        {
            SHA384 sha = new SHA384Managed();
            Byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(cadena));

            StringBuilder strBuilder = new StringBuilder();
            foreach (Byte b in hash)
            {
                strBuilder.AppendFormat("{0:x2}", b);
            }
            return strBuilder.ToString();
        }

        public static string generarClaveSHA512(string cadena)
        {
            SHA512 sha = new SHA512Managed();
            Byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(cadena));

            StringBuilder strBuilder = new StringBuilder();
            foreach (Byte b in hash)
            {
                strBuilder.AppendFormat("{0:x2}", b);
            }
            return strBuilder.ToString();
        }
    }
}
